package com.ruoyi.cms.controller;

import com.ruoyi.cms.domain.CategoryAccount;
import com.ruoyi.cms.service.ICategoryAccountService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 账号分类配置Controller
 *
 * @author lorry
 * @date 2020-11-30
 */
@Controller
@RequestMapping("/cms/account")
public class CategoryAccountController extends BaseController {
    private String prefix = "cms/account";

    @Autowired
    private ICategoryAccountService categoryAccountService;

    @RequiresPermissions("cms:account:view")
    @GetMapping()
    public String account() {
        return prefix + "/list";
    }

    /**
     * 查询账号分类配置列表
     */
    @RequiresPermissions("cms:account:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CategoryAccount categoryAccount) {
        startPage();
        List<CategoryAccount> list = categoryAccountService.selectCategoryAccountList(categoryAccount);
        return getDataTable(list);
    }

    /**
     * 导出账号分类配置列表
     */
    @RequiresPermissions("cms:account:export")
    @Log(title = "账号分类配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CategoryAccount categoryAccount) {
        List<CategoryAccount> list = categoryAccountService.selectCategoryAccountList(categoryAccount);
        ExcelUtil<CategoryAccount> util = new ExcelUtil<CategoryAccount>(CategoryAccount.class);
        return util.exportExcel(list, "account");
    }

    /**
     * 新增账号分类配置
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存账号分类配置
     */
    @RequiresPermissions("cms:account:add")
    @Log(title = "账号分类配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CategoryAccount categoryAccount) {
        return toAjax(categoryAccountService.insertCategoryAccount(categoryAccount));
    }

    /**
     * 修改账号分类配置
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        CategoryAccount categoryAccount = categoryAccountService.selectCategoryAccountById(id);
        mmap.put("categoryAccount", categoryAccount);
        return prefix + "/edit";
    }

    /**
     * 修改保存账号分类配置
     */
    @RequiresPermissions("cms:account:edit")
    @Log(title = "账号分类配置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CategoryAccount categoryAccount) {
        return toAjax(categoryAccountService.updateCategoryAccount(categoryAccount));
    }

    /**
     * 删除账号分类配置
     */
    @RequiresPermissions("cms:account:remove")
    @Log(title = "账号分类配置", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(categoryAccountService.deleteCategoryAccountByIds(ids));
    }
}
